package com.newasia.server.common.template.dataItem.basics;

import com.newasia.server.dal.entitys.MonitorDetail;
import jodd.util.StringUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class DataType {

	private Integer id;
	private String fcode;
	private String name;
	private Integer min;
	private Integer max;
	private String unit;
	private String meno;
	private String valueText;
	private Integer isShow;
	private Integer groupId;
	private Map<String,Object> value;
	private Formula formula;
	private List<Map<String,Object>> textMapList;

	public void setValueText(String valueText) {
		this.valueText = valueText.toUpperCase();
	}

	public void setTextMapList(List<Map<String, Object>> textMapList) {
		this.textMapList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> textMap: textMapList){
			this.textMapList.add(textMap);
		}
	}

	@Data
	private class Formula{
		private Integer type;
		private Integer offValue;
		private Integer divisor;
		//判断是否有所谓的除0以外的值代表同一状态
		private boolean flag;
		private Map<String,String> textMap;
	}

	public void setFormula(){
		if(isShow==0){
			return;
		}
		Formula formula = new Formula();
		List<Map<String, Object>> textMapList = new ArrayList<Map<String,Object>>();
		Map<String,String> map = new HashMap<String,String>();
		if (!StringUtil.isEmpty(valueText)) {
			if (valueText.contains("ALL:时间")) {
				//此为时间类型
				formula.setType(2);
			}else {
				//默认为滑动类型
				formula.setType(1);
				String[] texts = valueText.split(",");
				for (String text : texts) {
					//获取倍数
					if (text.contains("ALL:X")) {
						formula.setType(0);
						text = text.replaceAll("[^[A-Z]+$]", "");
						text = text.replaceAll("\\pP|\\pS", "");
						formula.setDivisor(StringUtil.isEmpty(text) ? 1 : Integer.valueOf(text));
						continue;
					}
					if (text.contains("OFF")) {
						formula.setOffValue(Integer.valueOf(text.split(":")[0]));
						continue;
					}
					String[] explain = text.split(":");
					Map<String, Object> textMap = new HashMap<String, Object>();
					if (explain[0].contains("ALL")) {
						map.put("OTHER", explain[1]);
						formula.setFlag(true);
						/*textMap.put("key", "OTHER");
						textMap.put("value", explain[1]);*/
					} else {
						Integer key = Integer.valueOf(explain[0].trim());
						if ((max == null || key <= max) && (min == null || key >= min)) {
							map.put(key.toString(), explain[1]);
							textMap.put("key", explain[0]);
							textMap.put("value", explain[1]);
							textMapList.add(textMap);
						}
					}
				}
				formula.setTextMap(map);
				this.setTextMapList(textMapList);
			}
		} else {//valuetext为"",必为整数数字
			formula.setType(0);
			formula.setDivisor(1);
		}
		this.formula = formula;
	}

	public void setValue(Integer pendingValue) {
		String key = pendingValue.toString();
		Map<String,Object> map = new HashMap<String,Object>();
		Formula formula = this.formula;
		Map<String,String> textMap = formula.getTextMap();
		if (formula.getType()==2){
			map.put("key",key);
			map.put("value",key);
		}else if(StringUtil.isEmpty(valueText)){
			map.put("key",key);
			map.put("value",Double.valueOf(key)/formula.getDivisor());
		}else if(formula.isFlag()){
			String value = textMap.get(key);
			if(StringUtil.isEmpty(value)){
				map.put("key",key);
				map.put("value",textMap.get("OTHER"));
			}else{
				map.put("key",key);
				map.put("value",value);
			}
		}else if (formula.getType()==1){
			map.put("key",key);
			map.put("value",textMap.get(key));
		}else if (formula.getType()==0){
			String value = textMap.get(key);
			if(StringUtil.isEmpty(value)){
				map.put("key",key);
				map.put("value",Double.valueOf(key)/formula.getDivisor());
			}else{
				map.put("key",key);
				map.put("value",value);
			}
		}
		this.value = map;
	}
}
